package com.itlong.cloud.controller.operate;

import com.itlong.cloud.POJO.DTO.operate.InterfaceDTO;
import com.itlong.cloud.POJO.PO.InterfacePO;
import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.POJO.VO.operate.InterfaceVO;
import com.itlong.cloud.annotation.UnAuthAccessRequired;
import com.itlong.cloud.annotation.UnParamEncrypt;
import com.itlong.cloud.enumerate.ecode.BaseErrorCodeEnum;
import com.itlong.cloud.project.operate.IOperateInterfaceServiceClient;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.utils.page.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * <desc>
 *      :运营-管理-接口模块
 * <desc>
 * @author LiChangXu
 * @date: 2021-08-13 18:03
 **/
@RestController
@RequestMapping
public class OperateInterfaceController {

    @Autowired
    IOperateInterfaceServiceClient iOperateInterfaceServiceClient;

    /**
     * <desc>
     *      :添加接口
     * <desc>
     * @param interfacePO
     * @return: java.lang.Object
     * @author LiChangXu
     * @date: 2021-08-16 8:32
     **/
    @UnAuthAccessRequired
    @UnParamEncrypt
    @RequestMapping(value = "/saveInterface",method = RequestMethod.POST)
    public Object save(InterfacePO interfacePO){
        MessageVO messageVO;
        try{
            iOperateInterfaceServiceClient.saveInterface(interfacePO);
            messageVO = new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch (Exception e){
            throw new DataAccessException("【】添加失败saves"+e);
        }
        return messageVO;
    }

    /**
     * <desc>
     *      :修改接口信息
     * <desc>
     * @param interfacePO
     * @return: java.lang.Object
     * @author LiChangXu
     * @date: 2021-08-16 8:33
     **/
    @UnAuthAccessRequired
    @UnParamEncrypt
    @RequestMapping(value = "/updateInterface",method = RequestMethod.POST)
    public Object update(InterfacePO interfacePO){
        if(StringUtils.isBlank(interfacePO.getInterfaceId()) || StringUtils.isBlank(interfacePO.getUpdateUserId())){
            return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
        }
        MessageVO messageVO;
        try{
            iOperateInterfaceServiceClient.updateInterface(interfacePO);
            messageVO = new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch (Exception e){
            throw new DataAccessException("【】添加失败updates"+e);
        }
        return messageVO;
    }

    /**
     * <desc>
     *      :逻辑删除接口
     * <desc>
     * @param interfaceId 接口编号 逻辑主键
     * @param updateUserId 登录用户id
     * @return: java.lang.Object
     * @author LiChangXu
     * @date: 2021-08-16 8:33
     **/
    @UnAuthAccessRequired
    @UnParamEncrypt
    @RequestMapping(value = "/deleteInterface",method = RequestMethod.POST)
    public Object delete(@RequestParam("interfaceId")String interfaceId,@RequestParam("updateUserId")String updateUserId){
        MessageVO messageVO;
        try{
            iOperateInterfaceServiceClient.deleteInterface(interfaceId,updateUserId);
            messageVO = new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch (Exception e){
            throw new DataAccessException("【】添加失败deletes"+e);
        }
        return messageVO;
    }

    /**
     * <desc>
     *      :条件-分页 查询
     * <desc>
     * @param interfaceName 接口名称
     * @param status 接口状态
     * @param startTime 查询接口开始时间
     * @param endTime 查询接口结束时间
     * @return: java.lang.Object
     * @author LiChangXu
     * @date: 2021-08-13 18:02
     **/
    @UnAuthAccessRequired
    @UnParamEncrypt
    @RequestMapping(value = "/getInterfaceListTest",method = RequestMethod.POST)
    public Object getList(@RequestParam("interfaceName")String interfaceName,
                          @RequestParam("status")int status,
                          @RequestParam("startTime")String startTime,
                          @RequestParam("endTime")String endTime){
        MessageVO messageVO;
        try {
            List<InterfacePO> list = iOperateInterfaceServiceClient.getInterfaceList(interfaceName,status,startTime,endTime);
            messageVO = new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),list);
        }catch (Exception e){
            throw new DataAccessException("【】获取列表失败",e);
        }
        return  messageVO;
    }

    /**
     * <desc>
     *      :获取分页列表
     * <desc>
     * @param interfaceDTO 接口dto
     * @return: java.lang.Object
     * @author LiChangXu
     * @date: 2021-08-13 16:08
     **/
    @UnAuthAccessRequired
    @UnParamEncrypt
    @RequestMapping(value = "/getInterfacePageList",method = RequestMethod.POST)
    public Object getInterfacePageList(InterfaceDTO interfaceDTO){
        MessageVO messageVO;
        try {
            Page<InterfaceVO> interfaceVOPage = iOperateInterfaceServiceClient.getInterfacePageList(interfaceDTO);
            messageVO = new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),interfaceVOPage);
        }catch (Exception e){
            throw new DataAccessException("【】获取列表失败",e);
        }
        return  messageVO;
    }

    /**
     * <desc>
     *      :更新启用-禁用状态
     * <desc>
     * @param interfaceId 接口id
     * @param status 接口状态
     * @param updateUserId 修改者id
     * @return: java.lang.Object
     * @author LiChangXu
     * @date: 2021-08-16 8:35
     **/
    @UnAuthAccessRequired
    @UnParamEncrypt
    @RequestMapping(value = "/updateStatus",method = RequestMethod.POST)
    public Object updateStatus(@RequestParam("interfaceId")String interfaceId,
                               @RequestParam("status")int status,
                               @RequestParam("updateUserId")String updateUserId){
        MessageVO messageVO;
        try{
            int n = iOperateInterfaceServiceClient.updateStatus(interfaceId,status,updateUserId);
            if(n==0){
                messageVO = new MessageVO(BaseErrorCodeEnum.ERROR.getErrorCode());
            }else {
                messageVO = new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
            }
        }catch (Exception e){
            throw new DataAccessException("【】状态更新失败",e);
        }
        return messageVO;
    }

    /**
     * <desc>
     *      :修改接口文档
     * <desc>
     * @param interfaceId 接口id
     * @param interfaceDocx 接口文档
     * @param updateUserId 修改者id
     * @return: java.lang.Object
     * @author LiChangXu
     * @date: 2021-08-16 8:36
     **/
    @UnAuthAccessRequired
    @UnParamEncrypt
    @RequestMapping(value = "/updateDocx",method = RequestMethod.POST)
    public Object updateDocx(@RequestParam("interfaceId")String interfaceId,
                             @RequestParam("interfaceDocx")String interfaceDocx,
                             @RequestParam("updateUserId")String updateUserId){
        MessageVO messageVO;

        try{
            iOperateInterfaceServiceClient.updateDocx(interfaceId,interfaceDocx,updateUserId);
            messageVO = new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch (Exception e){
            throw new DataAccessException("【】接口文档更新失败",e);
        }
        return messageVO;
    }
}
